<?php
class signup extends MY_Controller{

	function __construct(){
		parent::__construct();
		$this->config->load("raw");

	}
	
	function index(){
		$this->home();
//		echo "hellow";
	}
	
	function home(){
		$this->title = "Sign-Up";
					
		/*
		 * @todo : finalize view 
		 * @optional : when need jscript or css ADD jscript and css with js_script() css_script() 
		 * @param config page 
		 * @finalizeView : array(title,jscript_add,css_add,jscript,css,header,content,footer);
		 */
		$jscript = js_script(array("formcheck"));
		$css = $css = css_script(array("formcheck"));
		$page = $this->config->item('page');
		$page['title'] = $page['title']." - ".$this->title;
		$config = array(
			'title' => $page['title'],
			'jscript_add'=>$jscript,
			'css_add'=>$css,
    		'jscript'=>js_script($page['jscript_default']),
    		'css'=>css_script($page['css_default'])
		);
		
		$this->load->view("sas/login/signup",$config);
	}
	
	function regstudent(){
		$this->load->model('membermod');
		$this->load->model('memberinfomod');
		$this->load->model('membercontactmod');
		$this->load->model('memberstudentmod');	
		
		$first_name = $this->input->post("first_name");
		$last_name = $this->input->post("last_name");
		$born_place = $this->input->post('born_place');
//		$birthday = date("Y-m-d",$this->input->post('birthday'));
		$birthday = $this->input->post('birthday');
		$username = $this->input->post("username");
		$password = do_hash($this->input->post("password"), 'md5');
		
		$address = $this->input->post("address");
		$phone = $this->input->post("phone");
		$email = $this->input->post("email");
		$facebook = $this->input->post("facebook");
		$twitter = $this->input->post("twitter");
		
		$year = $this->input->post("year");
		$faculty = $this->input->post("faculty");
		$class = $this->input->post("class");
		
		$name = $first_name." ".$last_name;
				
		$images_tmp = $_FILES['images']['tmp_name'];
		$images_type = $_FILES['images']['type'];
		$large_height = 500;
		$medium_height = 300;
		$small_height = 200;
		$tiny_height = 100;
		$thumb_height = 80;
		
		$id = $this->membermod->generateID($first_name);
		$id_contact = $this->membercontactmod->generateID($first_name);
		$level = md5("student");
		
		if($images_type=="" || $images_type==NULL){
			$member = array(
						'id_member'=>$id,
						'username'=>$username,
						'password'=>$password,
						'level'=>$level
					);
		} else {
			$member = array(
						'id_member'=>$id,
						'username'=>$username,
						'password'=>$password,
						'level'=>$level,
						'images_large'=>$this->images_large($images_tmp,$large_height,$name,$images_type),
						'images_medium'=>$this->images_medium($images_tmp,$medium_height,$name,$images_type),
						'images_small'=>$this->images_small($images_tmp,$small_height,$name,$images_type),
						'images_tiny'=>$this->images_tiny($images_tmp,$tiny_height,$name,$images_type),
						'images_thumbs'=>$this->images_thumbs($images_tmp,$thumb_height,$name,$images_type),
						'images_type'=>$images_type
					);
		}
		
		$info = array(
					'id_member'=>$id,
					'first_name'=>$first_name,
					'last_name'=>$last_name,
					'born_place'=>$born_place,
					'birthday'=>$birthday
				);
		$contact = array(
						'id_member_contact'=>$id_contact,
						'id_member'=>$id,
						'address'=>$address,
						'phone'=>$phone,
						'email'=>$email,
						'facebook'=>$facebook,
						'twitter'=>$twitter,
					);
					
		$student = array(
						'id_member'=>$id,
						'year'=>$year,
						'faculty'=>$faculty,
						'class'=>$class
					);
					
		$this->membermod->add($member);
		$this->memberinfomod->add($info);
		$this->membercontactmod->add($contact);
		$this->memberstudentmod->add($student);
		
		redirect("sign");
		return true;
		
	}
	
}